package org.niux.aims.action.ajax;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.niux.aims.common.util.FileUtil;
import org.niux.aims.common.web.AimsBaseAction;
import org.niux.aims.vo.BackUpDataVO;

/**
 * 
 * Copyright (c) 2013,niux<br>
 * All rights reserved.<br>
 * 
 * 文件名称：GetBackUpByPage.java<br>
 * 摘    要：获取备份数据AJAX类<br>
 * <br>
 * 当前版本：<br>
 * 作    者：梁铭显 Email: <a href="mailto:liangmingxian@gmail.com">liangmingxian@gmail.com</a><br>
 * 完成日期：2013-9-24<br>
 * <br>
 * 取代版本：<br>
 * 原作者  ：<br>
 * 完成日期：2013-9-24<br>
 */
public class GetBackUpAjax extends AimsBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2199086875378566502L;
	private Log log = LogFactory.getLog(this.getClass());
	private int page;
	private int pageSize;
	private List<BackUpDataVO> backUpList;
	
	public GetBackUpAjax(){
		this.backUpList = new ArrayList<BackUpDataVO>();
	}
	
	/**
	 * 
	 * 方法概述：分页获取备份文件
	 * 描述：分页获取备份文件
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显 
	 * 修改时间：2013-9-24
	 */
	public String getBackUpByPage(){
		log.debug(page + "====" + pageSize);
		backUpList.clear();
		
		int startPage = page * pageSize;
		
		FileUtil fileUtil = new FileUtil();
		String path = ServletActionContext.getServletContext().getRealPath("backup");
		File[] fileList = fileUtil.getFileList(path);
		fileList = fileList == null ? new File[0] : fileList;
		
		int num = 0;
		int count = 0;
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		List<BackUpDataVO> list = new ArrayList<BackUpDataVO>();
		
		for (File file : fileList) {
			if(num >= startPage && count < pageSize){
				BackUpDataVO backUp = new BackUpDataVO();
				backUp.setFileName(file.getName());

				Calendar cd = Calendar.getInstance();
		        cd.setTimeInMillis(file.lastModified());
		        
				backUp.setBackUpTime(dateFormat.format(cd.getTime()));
				list.add(backUp);
				count++;
			}
			num++;
		}
		
		backUpList = sort(list);
		
		return SUCCESS;
	}
	
	/**
	 * 
	 * 方法概述：根据时间倒序排列
	 * 描述：根据时间倒序排列
	 * @param datas
	 * @return
	 * 返回类型：List<BackUpDataVO>
	 * 修改人：梁铭显 
	 * 修改时间：2013-9-25
	 */
	public List<BackUpDataVO> sort(List<BackUpDataVO> datas){
		List<Long> tempList = new ArrayList<Long>();
		
		for (BackUpDataVO data : datas) {
			long time = Long.parseLong(data.getBackUpTime().replace(" ", "").replaceAll("-", "").replaceAll(":", ""));
			tempList.add(time);
		}
		
		Collections.sort(tempList,Collections.reverseOrder());
		
		List<BackUpDataVO> sortBy = new ArrayList<BackUpDataVO>();
		
		for(long l : tempList){
			for (BackUpDataVO data : datas) {
				long time = Long.parseLong(data.getBackUpTime().replace(" ", "").replaceAll("-", "").replaceAll(":", ""));
				if(l == time){
					sortBy.add(data);
					break;
				}
			}
		}
		
		return sortBy;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public List<BackUpDataVO> getBackUpList() {
		return backUpList;
	}

	public void setBackUpList(List<BackUpDataVO> backUpList) {
		this.backUpList = backUpList;
	}
}
